package org.bdware.doip.endpoint.doipServer;

import com.google.gson.Gson;
import org.apache.log4j.Logger;

import java.net.URI;
import java.util.HashMap;

public class DoipListenerGenerator {

    static Logger logger = Logger.getLogger(NettyDoipListener.class);
    static HashMap<String,DoipListener> adhocListeners = new HashMap<>();

    public static DoipListener CreateDoipListener(DoipListenerInfo listenerInfo) {
        try {
            URI uri = new URI(listenerInfo.url);
            logger.debug("[URI Parse]scheme:" + uri.getScheme() + "; host: " + uri.getHost() + "  port: " + uri.getPort());
            switch (uri.getScheme()) {
                case "tcp":
                case "TCP":
                case "udp":
                case "UDP":
                case "tls":
                case "TLS":
                case "ws":
                case "WS":
                    return NettyDoipListener.CreateDoipListener(listenerInfo);
                default:
                    if(adhocListeners.get(uri.getScheme())!= null )return adhocListeners.get(uri.getScheme());
                    logger.error("[Create repository error] Unsupported Listener: " + new Gson().toJson(listenerInfo));
                    return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void addListener(String schema, DoipListener listener){
        adhocListeners.put(schema,listener);
    }
}
